package com.web.controller.system;

import com.domain.system.Dept;
import com.github.pagehelper.PageInfo;
import com.service.system.DeptService;
import com.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo pageInfo =  deptService.findAll(companyId, page, size);
        request.setAttribute("page", pageInfo);
        return "system/dept/dept-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        return "system/dept/dept-add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        Dept dept = deptService.findById(id);
        request.setAttribute("dept", dept);
        return "system/dept/dept-update";
    }

    @RequestMapping("/edit")
    public String edit(Dept dept){
        dept.setCompanyId(companyId);
        dept.setCompanyName(companyName);
        if (StringUtils.isBlank(dept.getId())) {
            Integer result = deptService.save(dept);
        }else {
//            更新
            Integer result = deptService.update(dept);
        }
        return "redirect:/system/dept/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Integer result = deptService.delete(id);
        return "redirect:/system/dept/list.do";
    }



}
